<?php
class Admin_category extends MY_Controller {
	public function __construct()
	{
		$this->data["menu"] = "category";
		parent::__construct();		
		$this->data["base_link"] = base_index_admin . "category/";
	}
	
	public function index()
	{	
		$pgintf = "";
		$listbody = "";
		$total_num = $this->categoryModel->count_all();
		if($total_num > 0){
			list($pgintf, $startnum) = createPaging($_SERVER["PHP_SELF"], strip_tags(@$_GET["page"]), $total_num, $this->data["paging_value"], $_SERVER["QUERY_STRING"]);
			$i = 1;
			$result = $this->categoryModel->get_all($this->data["paging_value"], $startnum, "category.category_id DESC", array("lan" => $this->data["language"]));			
			foreach($result as $category){
				$visible = $category->visible;
				$visibleview = array_single_select($this->data["arr_visible"], $visible);
				$visibleclass = "";
				if($visible == "y"){
					$visibleclass = "class=\"on\"";
				}elseif($visible == "n"){
					$visibleclass = "class=\"off\"";
				}
								
				$listbody .= "<tr>\n";
				$listbody .= "<td>{$category->category_id}</td>\n";		
				$listbody .= "<td>{$category->categoryName}</td>\n";
				$listbody .= "<td>{$category->position}</td>\n";
				$listbody .= "<td {$visibleclass}>{$visibleview}</td>\n";
				$listbody .= "<td><a href=\"".$this->data["base_link"]."edit/{$category->category_id}\"><img src='".base_common_admin."images/ico_edit.gif' /></a></td>\n";
				$listbody .= "<td><a href=\"javascript:void(0);\" onclick=\"delete_confirm({$category->category_id});\"><img src='".base_common_admin."images/ico_drop.gif' /></a></td>\n";
				$listbody .= "</tr>\n";
			}
		}else{
			$listbody .= "<tr>\n";
			$listbody .= "<td colspan='6'>".$this->data["lang"]["general"]["noresult"]."</td>\n";
			$listbody .= "</tr>\n";
		}
		
		$this->data["pgintf"] = $pgintf;
		$this->data["listbody"] = $listbody;
		@$this->data["msg"] = $_SESSION["msg"];
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/category/category_list', $this->data);		
	}
	
	public function add()
	{	
		@$visible 	= $_SESSION["visible"];
		@$position 	= $_SESSION["position"];			
		
		$arr_err = array();
		if(!empty($_SESSION["err_lang"])) {
			foreach($this->data["arr_lang"] as $la => $val){
				$lang_key = lang_key($la, "category_url");
				if(in_array($lang_key, $_SESSION["err_lang"])){
					$arr_err[$lang_key] = "<br /><span class=\"required\">" . $this->data["lang"]["category"]["errormsg_category_url"] . "</span>";
				}
			}
		}
		
		$visible = !empty($visible) ? $visible : "y";
		$visibleradio = @array_radio($this->data["arr_visible"], $visible, "visible");
		
		unset($_SESSION["category_id"]);
		unset($_SESSION["position"]);
		unset($_SESSION["visible"]);
		unset($_SESSION["err"]);
		
		$lang_fields = lang_createFields($this->data["arr_lang"], $this->data["arr_fields_category"], $this->data["arr_lang_chk_empty"], $this->data["lang"]["general"]["err_empty"], $arr_err);
		
		$this->data["visibleradio"] = $visibleradio;
		$this->data["position"] = $position;
		$this->data["lang_fields"] = $lang_fields;
		
		$this->load->view('admin/category/category_add', $this->data);		
	}
	
	public function edit()
	{
		if(empty($_SESSION["category_id"])){
			$category_id = $this->uri->segment(4);
			$query = $this->categoryModel->get_one($category_id);
			if($query->num_rows < 1)
				header("Location: " . $this->data["base_link"] . "add");
			$category = $query->row_array();
			$query->free_result();
			extract($category);
			$table = "categoryLan";
			$where = "`category_id` = '$category_id'";	
			lang_addData($table, $where, $this->data["arr_fields_category"], $this->data["arr_lang"]);
		}
		else{
			extract($_SESSION);
		}
				
		$arr_err = array();
		if(!empty($_SESSION["err_lang"])) {
			foreach($this->data["arr_lang"] as $la => $val){
				$lang_key = lang_key($la, "category_url");
				if(in_array($lang_key, $_SESSION["err_lang"])){
					$arr_err[$lang_key] = "<br /><span class=\"required\">" . $this->data["lang"]["category"]["errormsg_category_url"] . "</span>";
				}
			}
		}
		
		$visible = !empty($visible) ? $visible : "y";
		$visibleradio = @array_radio($this->data["arr_visible"], $visible, "visible");
					
		$lang_fields = lang_createFields($this->data["arr_lang"], $this->data["arr_fields_category"], $this->data["arr_lang_chk_empty"], $this->data["lang"]["general"]["err_empty"], $arr_err);
		
		$this->data["category_id"] = $category_id;
		$this->data["visibleradio"] = $visibleradio;
		$this->data["position"] = $position;
		$this->data["lang_fields"] = $lang_fields;
		@$this->data["msg"] = $_SESSION["msg"];

		unset($_SESSION["category_id"]);
		unset($_SESSION["position"]);
		unset($_SESSION["visible"]);
		unset($_SESSION["err"]);
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/category/category_edit', $this->data);		
	}
	
	public function done()
	{
		if(empty($_POST)){
			header("Location: " . $this->data["base_link"] . "add/");
			exit();
		}
			
		POST_To_SESSION();		
		lang_checkErrEmpty($this->data["arr_lang"], $this->data["arr_fields_category"], $this->data["arr_lang_chk_empty"]);

		extract($_POST);
				
		$arr = array();
		$linkret = $this->data["base_link"] . "add/";
		if(!empty($category_id)){
			$arr["category_id !="] = $category_id;
			$linkret = $this->data["base_link"] . "edit/" . $category_id;
		}

		$err_lang = array();		
		foreach($this->data["arr_lang"] as $la => $val){
			if(!empty($_POST["category_url_" . $la])){
				$arr["lan"] = $la;
				$arr["category_url"] = $_POST["category_url_" . $la];
				if($this->categoryModel->check_url($arr)->num_rows > 0){
					$err_lang[] = lang_key($la, "category_url");
				}					
			}
			else{
				$url = stripUnicode($_SESSION["categoryName_" . $la]);
				if(!empty($url)){
					$arr["lan"] = $la;
					$arr["category_url"] = $url;
					if($this->categoryModel->check_url($arr)->num_rows > 0){
						unset($arr["category_url"]);
						$url = $url . "-";
						$str = "";						
						if(!empty($category_id)){							
							$str = $this->categoryModel->check_old_url($url, array("lan" => $la, "category_id" => $category_id));							
						}
						if(empty($str)){
							$query = $this->categoryModel->get_max_url($url, $arr);
							$num = 1;
							if($query->num_rows > 0){
								$row = $query->row();
								$num = end(explode("-", $row->category_url));
								$num++;
							}							
							$url .= $num;							
						}
						else
							$url = $str;
					}
					$_SESSION["category_url_" . $la] = $url;
				}
			}
		}
		$err = "";
		$_SESSION["err"] = $err;
		$_SESSION["err_lang"] = array_merge($_SESSION["err_lang"], $err_lang);
		if(!empty($_SESSION["err_lang"]) || !empty($err)){
			header("Location: " . $linkret);
			exit();
		}
		
		$data = array();
		$data["position"] = intval($position);
		$data["visible"] = $visible;
		if(empty($category_id)){
			$category_id = $this->categoryModel->insert_entry_position($data);
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["regist_msg"] . "</p>";
		}
		else{
			$this->categoryModel->update_entry($category_id, $data);		
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["upload_done"] . "</p>";
		}
		
		$def = array("category_id" => $category_id);	
		$table = "categoryLan";
		$colID = "categoryLan_id";
		lang_accesstData($table ,$this->data["arr_lang"], $this->data["arr_fields_category"], $def, $this->data["arr_lang_chk_empty"], $colID);
			
		//unset
		unsetPOSTSESSION();
		unset($_SESSION["err_lang"]);
		unset($_SESSION["err"]);		
		
		header("Location: " . $this->data["base_link"] . "edit/" . $category_id);
	}
	
	function delete(){
		@$id = $_POST["id"];
		@$page = $_POST["page"];		
		if(!empty($id)){
			$this->categoryModel->delete_entry($id);
			$pageView = "";
			if(!empty($page))
				$pageView = "?page=" . $page;
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["delete_msg"] . "</p>";
			header("Location: " . $this->data["base_link"] . $pageView);
		}
	}
}